import maya.cmds as mc
import sys

class seperateBlend():
    def seperateBlendUI(self):
        seperateBlendUI= 'seperateBlendUI'
        if mc.window(seperateBlendUI,ex = True):
            mc.deleteUI(seperateBlendUI)
            
        mc.window(seperateBlendUI,widthHeight=(360, 400),t='facial RigSys Window_v1.01',menuBar = True,rtf=True,s=False)
        mc.rowColumnLayout('lostMain',numberOfColumns=1,w=360,cal = (5,'center'),m=True)
            
        mc.textScrollList('blendshapelist',append=[],w=360,allowMultiSelection=True)
        mc.setParent('..')
        mc.rowColumnLayout('buttonly',numberOfColumns=3,w=360,cal = (5,'center'),m=True)
        mc.button(l='add to list',c=lambda *args:self.addToScroll(),w=120)
        mc.button(l='clear list',c=lambda *args:self.clearList(),w=120)
        mc.button(l='remove select from list',c=lambda *args:self.removefromlist(),w=120)
        
        mc.setParent('..')
        mc.rowColumnLayout('originally',numberOfColumns=2,w=300,cal = (5,'center'),m=True)
        mc.textField('originalMesh',w=260)
        mc.button(l='add original mesh',w=100,c=lambda *args:self.addOriMesh())
        mc.button('excude',c=lambda *args:self.duplicateOri())
        mc.button('blend',c=lambda *args:self.createBlend())
            
        mc.showWindow(seperateBlendUI)
        
    
    def addToScroll(self):
        selobj=mc.ls(sl=True)
        objadd=[]
        seleIterm=mc.textScrollList('blendshapelist',ai=True,q=True)
        print(seleIterm)
        for i in selobj:
            print(1)
            if seleIterm==None:
                objadd.append(i)
                mc.select(i,add=True)
            else:
                mc.select(seleIterm)
                mc.select(selobj,add=True)
        objaddlist=mc.ls(sl=True)
        mc.textScrollList('blendshapelist',ra=True,e=True)
        mc.textScrollList('blendshapelist',append=objaddlist,e=True)
        mc.select(selobj)
    def clearList(self):
        mc.textScrollList('blendshapelist',ra=True,e=True)
    def removefromlist(self):
        selobj=mc.ls(sl=True)
        needDeleteii=[]
        seleIterm=mc.textScrollList('blendshapelist',sii=True,q=True)
        
        seleIterm=seleIterm[::-1]
        
        for i in seleIterm:
       
            mc.textScrollList('blendshapelist',rii=i,e=True)
    def addOriMesh(self):
        oriMesh=mc.ls(sl=True)[0]
        mc.textField('originalMesh',text=oriMesh,e=True)
    def duplicateOri(self):
        orimesh=mc.textField('originalMesh',text=True,q=True)
        mc.duplicate(orimesh,n='blendBridge')
        mc.move(10,0,0,'blendBridge',relative=True, objectSpace=True, worldSpaceDistance=True)
    
        targetBlend=mc.textScrollList('blendshapelist',ai=True,q=True)
        orimesh=mc.textField('originalMesh',text=True,q=True)
        mc.duplicate(orimesh,n='blendreverseBridge')
        if len(targetBlend)==0:
            sys.stdout.write('settings has existed , only one setting exists at one scene')
    
        if len(targetBlend)>=1:
            mc.select(targetBlend)
            mc.select('blendBridge',add=True)
            #for i in targetBlend:
            mc.blendShape(n='seperateBlend')
            
    def createBlend(self):
            targetAttr=mc.blendShape('seperateBlend',target=True,q=True)
            
            
            argnum=1
            for ta in targetAttr:
                mc.setAttr('seperateBlend.'+str(ta),1)
                mc.duplicate('blendBridge',n=str(ta)+'_L')
                mc.move(10*argnum,0,0,str(ta)+'_L',r=True)
                mc.setAttr('seperateBlend.'+str(ta),0)
                argnum=argnum+1
                
                mc.select(str(ta)+'_L')
                mc.select(str(ta),add=True)
                mc.select('blendreverseBridge',add=True)
    
                mc.blendShape(n='seperatereveseBlend')
                mc.setAttr('seperatereveseBlend.'+str(ta),1)
                mc.setAttr('seperatereveseBlend.'+str(ta)+'_L',-1)
                mc.duplicate('blendreverseBridge',n=str(ta)+'_R')
                
                leftX=mc.getAttr(str(ta)+'_L.tx')
                leftY=mc.getAttr(str(ta)+'_L.ty')
                leftZ=mc.getAttr(str(ta)+'_L.tz')
                
                mc.setAttr(str(ta)+'_R.tx',leftX)
                mc.setAttr(str(ta)+'_R.ty',leftY)
                mc.setAttr(str(ta)+'_R.tz',leftZ+5)
                
                mc.delete('seperatereveseBlend')  